package cc.wsyw126.java.collection.list;

import java.util.*;

/**
 * Created by junpeng.yang on 16/8/26.
 */
public class TestList {

    /**
     * 测试listIterator
     */
    public void testList(){
        ArrayList<Integer> arrayList = new ArrayList<>(Arrays.asList(1,2,3,4,5,3,2,34,43,3,434));
        ListIterator<Integer> sub = arrayList.listIterator();
        ListIterator<Integer> sub3 = arrayList.listIterator(3);

        while (sub.hasNext()){
            System.out.print(" " + sub.next());
        }
        System.out.println();
        while (sub3.hasNext()){
            System.out.print(" " + sub3.next());
        }

        System.out.println();
        while (sub.hasPrevious()){
            System.out.print(sub.previousIndex() + " " + sub.previous()+"\t");
        }
        System.out.println();
    }

    public void testLinkedList(){
        LinkedList<Integer> linkedList = new LinkedList<>(Arrays.asList(new Integer[]{12,121,2,3,23,24,343,434,34,3,42,34232,32,3,23,12,12,1,32,43,4,35,34,2,342,31,245,3,4523,42,341,234,1234,2134,21,4323,4,1234,12,341,234}));
        ArrayList<Integer> arrayList = new ArrayList<>(Arrays.asList(new Integer[]{12,121,2,3,23,24,343,434,34,3,42,34232,32,3,23,12,12,1,32,43,4,35,34,2,342,31,245,3,4523,42,341,234,1234,2134,21,4323,4,1234,12,341,234}));
        int size = linkedList.size(),n=10000000;
        Random random = new Random();
        arrayList.add(123);
        long start = System.currentTimeMillis();
        for (int i = 0; i < n; i++) {
            linkedList.get(random.nextInt(size));
        }
        System.out.println("(System.currentTimeMillis() - start) = " + (System.currentTimeMillis() - start));

        start = System.currentTimeMillis();
        for (int i = 0; i < n; i++) {
            arrayList.get(random.nextInt(size));
        }
        System.out.println("(System.currentTimeMillis() - start) = " + (System.currentTimeMillis() - start));

    }

    public static void main(String[] args) {
        TestList testList = new TestList();
        testList.testList();

        System.out.println("\n----------------------------");
        testList.testLinkedList();
    }
}
